﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Common.Tools
{
    public static class Extensions
    {
        /// <summary>
        ///     数值转化为字符串，以scale进制转换
        /// </summary>
        /// <param name="number"></param>
        /// <param name="scale"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string ComplexString(long number, int scale, int length = 0)
        {
            var array = new List<string>();
            if (scale < 2 || scale > 36)
            {
                throw new ArgumentException();
            }
            string strRet = "";

            long temp = number;

            while (temp > 0)
            {
                var r = (int)(temp % scale);
                if (r < 10) array.Add(r.ToString());
                else
                {
                    int a = Convert.ToInt32('a');
                    int c = a + r - 10;
                    char ch = Convert.ToChar(c);
                    array.Add(ch.ToString().ToUpper());
                }
                temp = temp / scale;
            }

            array.Reverse();

            foreach (string str in array)
            {
                strRet += str;
            }

            if (strRet.Length < length)
            {
                for (int i = strRet.Length; i < length; i++)
                {
                    strRet = "0" + strRet;
                }
            }
            return strRet;
        }

        /// <summary>
        ///     字符转化为数值
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        private static int ConvertNum(char ch)
        {
            int ivalue;
            if (int.TryParse(ch.ToString(), out ivalue))
            {
                return ivalue;
            }
            int tValue = Convert.ToInt32(Char.ToUpper(ch));
            int tL = Convert.ToInt32('A');
            return tValue - tL + 10;
        }

        /// <summary>
        ///     字符串转化为对应位的数字
        /// </summary>
        /// <param name="strNum"></param>
        /// <returns></returns>
        public static long ComplexNumeric(string strNum, int scale)
        {
            if (scale < 2 || scale > 36 || strNum.Length == 0)
            {
                throw new ArgumentException();
            }
            long retDat = 0;

            foreach (char ch in strNum)
            {
                int n = ConvertNum(ch);
                if (n < 0 || n >= scale)
                {
                    throw new ArgumentException();
                }
                retDat = (retDat * scale + n);
            }
            return retDat;
        }
        /// <summary>
        /// 获取总星数
        /// </summary>
        /// <param name="totalStar"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static decimal GetStar(int totalStar, int count)
        {
            if (count != 0)
            {
                decimal total = Math.Round((decimal)totalStar / count, 2);
                var _total = total.ToString();
                //  正则验证是否为数字
                Regex reg = new Regex("^\\d+$");//设置正则表达式 匹配所有字符都为数字                
                if (reg.IsMatch(_total))  //判断是否匹配成功
                {
                    //是整数
                    return total;
                }
                else
                {
                    //不是整数
                    var oldTotal = _total.Substring(_total.IndexOf(".") + 1, 1);
                    var newtotal = _total.Substring(_total.IndexOf(".") - 1, 1);
                    if (int.Parse(oldTotal) >= 5)
                    {
                        if (int.Parse(newtotal) >= 5)
                        {
                            return 5;
                        }
                        else
                        {
                            return Convert.ToDecimal(newtotal) + 1;
                        }
                    }
                    else
                    {
                        return Convert.ToDecimal(newtotal + ".5");
                    }
                }


            }
            else
            {
                return 0;

            }
            //if (count != 0)
            //{
            //    decimal TotalStarLevelRate = Math.Round((decimal) totalStar/count, 2);
            //    if (TotalStarLevelRate == 0)
            //    {
            //        return 0;
            //    }
            //    else if (TotalStarLevelRate > 0 && TotalStarLevelRate <= (decimal) 0.5)
            //    {
            //        return Convert.ToDecimal(0.5);
            //    }
            //    else if (TotalStarLevelRate > (decimal) 0.5 && TotalStarLevelRate <= (decimal) 1)
            //    {
            //        return 1;
            //    }
            //    else if (TotalStarLevelRate > (decimal) 1 && TotalStarLevelRate <= (decimal) 1.5)
            //    {
            //        return Convert.ToDecimal(1.5);
            //    }
            //    else if (TotalStarLevelRate > (decimal) 1.5 && TotalStarLevelRate <= (decimal) 2)
            //    {
            //        return 2;
            //    }
            //    else if (TotalStarLevelRate > (decimal) 2 && TotalStarLevelRate <= (decimal) 2.5)
            //    {
            //        return Convert.ToDecimal(2.5);
            //    }
            //    else if (TotalStarLevelRate > (decimal) 2.5 && TotalStarLevelRate <= (decimal) 3)
            //    {
            //        return 3;
            //    }
            //    else if (TotalStarLevelRate > (decimal) 3 && TotalStarLevelRate <= (decimal) 3.5)
            //    {
            //        return Convert.ToDecimal(3.5);
            //    }
            //    else if (TotalStarLevelRate > (decimal) 3.5 && TotalStarLevelRate <= (decimal) 4)
            //    {
            //        return 4;
            //    }
            //    else if (TotalStarLevelRate > (decimal) 4 && TotalStarLevelRate <= (decimal) 4.5)
            //    {
            //        return Convert.ToDecimal(4.5);
            //    }
            //    else
            //    {
            //        return 5;
            //    }
            //}
            //else
            //{
            //    return 0;
            //}
        }
    }
}
